<template>
  <view class="login-container">
    <!-- 登录提示图标 -->
    <uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
    <!-- 登录按钮 -->
    <!-- <button class="login-btn" type="warn" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button> -->
    <button class="login-btn" type="warn" @click="getUserInfo">一键登录</button>
    <!-- 登录提示 -->
    <text class="login-txt">登录后享受更多权益</text>
  </view>
</template>

<script>
    import { mapMutations, mapState } from 'vuex'
  
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    computed: {
      ...mapState('m_user', ['redirectInfo'])
    },
    methods: {
      ...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedictInfo']),
      // 点击登录 获取用户基本信息
      async getUserInfo() {
        const res = await uni.getUserProfile({
          desc: '登录'
        })
        this.updateUserInfo(res[1].userInfo)
        // 将对应的值传递给getToken
        this.getToken(res[1])
    },
    // 获取code对应的值
    async getToken(info) {
      const [err, res] = await uni.login({
        
      }).catch(err => err)
      console.log(res);
      if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败')
      // 准备参数对象
      const query = {
        code: res.code,
        encryptedData: info.encryptedData,
        iv: info.iv,
        rawData: info.rawData,
        signature: info.signature
      }
      // 获取token
      const { data: loginResult } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
      // if (loginResult.meta.status !== 200) return uni.$showMsg('登录失败')
      // uni.$showMsg('登录成功')
      console.log(loginResult);
      // 保存token值
      this.updateToken('love')
      this.navigateBack()
    },
    // 重定向
    navigateBack() {
      if (this.redirectInfo && this.redirectInfo.openType === 'switchTab') {
        uni.switchTab({
          url: this.redirectInfo.from,
          complete: () => {
            this.updateRedictInfo(null)
          }
        })
      }
    }
  },
  }
</script>

<style lang="scss" scoped>
.login-container{
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 750rpx;
  overflow: hidden;
  background-color: white;
  // &::after{
  //   position: absolute;
  //   bottom: 0;
  //   left: 0;
  //   content: '';
  //   display: block;
  //   width: 100%;
  //   height: 40px;
  //   background-color: #f8f8f8;
  //   border-radius: 100%;
  //   transform: translateY(50%);
  // }
  .login-btn{
    width: 90%;
    margin: 15px 0;
    border-radius: 25px;
    
  }
  .login-txt{
    color: gray;
  }
}
</style>